<?php
/**
 * Created by PhpStorm.
 * Login: Administrator
 * Date: 2017-11-01
 * Time: 09:19
 */
namespace app\index\controller;
use app\index\model\Jurisdiction;
use think\Db;
use think\Request;

class Role extends Base
{
    //<!--自动渲染-->//<!--方法渲染-->
    /*public function index_a()
    {
        $this->baseindex();
        $role = Db::table('oa_role')->where('state=1')->select();
        return $role;
    }
    public function index_x()
    {
        $this->baseindex();
        $role = Db::table('oa_role')->where('state=1')->select();
        $count = Db::table('oa_role')->where('state=1')->count();
        $roles['code'] = 0;
        $roles['msg'] = '';
        $roles['count'] = $count;
        $roles['data'] = $role;
        return $roles;
    }*/

    public function index()
    {
        //角色列表
        $this->baseindex();
        $role = Db::table('oa_role')->where('state=1 or state=2')->select();
        $this->assign('role', $role);
        return $this->fetch();
    }

    public function roleadd()
    {
        //渲染角色添加页面及权限数据
        $this->baseindex();
        $obj = new Jurisdiction();
        $jurisdiction = $obj->jurisdiction();
        $this->assign('jurisdiction', $jurisdiction);
        return $this->fetch();
    }

    public function roleadd_sub()
    {
        $this->baseindex();
        //添加角色
        if (isset($_POST['submit']) && $_POST['submit'] == "添加") {
            $res = input('post.');
            $model = new  Jurisdiction();
            $ress = $model->role_add($res);
            if ($ress) {
                $this->redirect('Role/index');
            } else {
                header('location: ' . $_SERVER['HTTP_REFERER']);
            }
        }
    }

    public function rolesave()
    {
        //修改权限页面及数据
        $this->baseindex();
        if (isset($_GET['id'])) {
            $id = input('get.id');
            $obj = new Jurisdiction();
            //qu权限数据（并处理原来拥有的权限和所有的权限）
            $res_jurisdiction = $obj->jurisdiction_role($id);
            //dump($res_jurisdiction);die;
            $role = $obj->role_message($id);
            $this->assign('jurisdiction', $res_jurisdiction);//全部权限、原来拥有的权限
            $this->assign('role', $role);
        }
        return $this->fetch();
    }

    public function rolesave_sub()
    {
        $this->baseindex();
        //处理角色权限提交的数据
        if (isset($_POST['submit']) && $_POST['submit'] == "submit") {
            //layui框架传
            $jurisdiction = '';
            $jurisdictionss = input('post.data');
            $jurisdiction = json_decode($jurisdictionss, true);
            //form表单传
            /*$jurisdiction = Request::instance()->except('submit,id,role,descript');
            $arr['id'] = input('post.id');
            $arr['role'] = input('post.role');
            $arr['descript'] = input('post.descript');*/
            //dump($jurisdiction);
            //dump($arr);die;
            $obj = new Jurisdiction();
            //$result = $obj->role_save($arr, $jurisdiction);
            $result = $obj->role_save($jurisdiction);
            return $result;
            /*if ($result==1) {
                return $result;
                //$this->redirect('Role/index');
            } else {
                return 0;
                //header('location: ' . $_SERVER['HTTP_REFERER']);
            }*/
        }
    }

    public function roledelete()
    {
        $this->baseindex();
        if (isset($_POST['submit_role_id']) && $_POST['submit_role_id'] == 'submit') {
            $role_id = input('post.id');
            $role_id_del = Db::table('oa_role')->field('state')->where(['id' => $role_id])->find();
            if ($role_id_del['state'] == 2) {
                return 1;
            } else {
                $ida = Db::table('oa_role')->where(['id' => $role_id])->update(['state' => 0]);
                //oa_role_jurisdiction关联的这个表角色数据修改状态
                /*$id_role_jurisdiction = Db::table('oa_role_jurisdiction')->where('role_id=' . $role_id)->update(['state' => 0]);
                //oa_user_role关联的这个表角色数据修改状态
                $id_user_role = Db::table('oa_user_role')->where('role_id=' . $role_id)->update(['state' => 0]);
                $num = $ida + $id_role_jurisdiction + $id_user_role;*/
                return $ida;
            }

        }
    }

    public function rolesave_descript()
    {
        //修改角色描述
        $this->baseindex();
        if (isset($_POST['submit']) && $_POST['submit'] == 'submit') {
            $id = input('post.id');
            $value = input('post.value');
            $key = input('post.field');
            $num = '';
            if ($key == 'role') {
                if (!empty($value)) {
                    $res = Db::table('oa_role')->where(['role' => $value])->select();
                    if (count($res) == 1) {
                        if ($id == $res[0]['id']) {
                            $where['id'] = $id;
                            $where[$key] = $value;
                            $num = Db::table('oa_role')->update($where);
                            return $num;
                        } else {
                            return $num;
                        }
                    } elseif (count($res) == 0) {
                        $where[$key] = $value;
                        $where['id'] = $id;
                        $num = Db::table('oa_role')->update($where);
                        return $num;
                    } else {
                        return $num;
                    }
                } else {
                    return 2;
                }
            } else {
                $where[$key] = $value;
                $where['id'] = $id;
                $num = Db::table('oa_role')->update($where);
                return $num;
            }
        }
    }
}